package com.infoguia;

import com.infoguia.util.ImageManager;
import com.infoguia.db.InfoguiaDao;
import com.infoguia.to.CategoryTo;
import com.infoguia.to.CompanyTo;
import com.infoguia.to.PhoneTo;
import com.infoguia.util.Constants;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class DetailItem extends ListActivity {

	private static final int ACTIVITY_DETAIL_ITEM = 3;
	public static String ID_COMPANY;
	private String mail;
	private String web;
	private String phone;
	private String geo;
	private InfoguiaDao dao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detailitem);

		this.paintDetail();
		this.configClicks();
		this.goHome();
	}

	private void paintDetail() {

		try {
			CompanyTo company;
			String[] arrayTemp;
			String strTemp;
			int i;

			if (dao == null) {
				dao = new InfoguiaDao(this);
				dao.open();
			}

			company = dao.getCompanyFull(Long.parseLong(ID_COMPANY));

			TextView text = (TextView) findViewById(R.id.nameDetail);
			text.setText(company.getName());

			text = (TextView) findViewById(R.id.desDetail);
			text.setText(company.getDescription());

			if (company.getCategory() != null) {
				arrayTemp = new String[company.getCategory().size()];
				i = 0;
				for (CategoryTo cat : company.getCategory()) {
					arrayTemp[i++] = cat.getName();
				}
				setListAdapter(new ArrayAdapter<String>(this,
						R.layout.detail_row, R.id.catDetail, arrayTemp));
			}

			strTemp = "";
			if (company.getNumbers() != null) {
				for (PhoneTo phone : company.getNumbers()) {
					strTemp += "\t" + phone.getCode() + "-" + phone.getNumber()
							+ "\n";
				}
			}
			text = (TextView) findViewById(R.id.telDetail);
			text.setText(strTemp);

			text = (TextView) findViewById(R.id.dirDetail);
			text.setText(company.getAddress());

			ImageView image;

			if (company.getLogo() != null
					&& company.getLogo().trim().length() != 0) {
				String path = "logos/" + company.getLogo();
				image = (ImageView) findViewById(R.id.imageCom);
				image.setImageBitmap(ImageManager
						.getBitmapFromAsset(path, this));
			} else {
				image = (ImageView) findViewById(R.id.imageCom);
				image.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
				image.setVisibility(View.INVISIBLE);
			}

			if (company.getMail() == null
					|| company.getMail().trim().length() == 0) {
				image = (ImageView) findViewById(R.id.mail);
				image.setVisibility(View.INVISIBLE);
			} else {
				mail = company.getMail();
			}
			if (company.getWeb() == null
					|| company.getWeb().trim().length() == 0) {
				image = (ImageView) findViewById(R.id.web);
				image.setVisibility(View.INVISIBLE);
			} else {
				web = company.getWeb();
			}
			if (company.getNumbers() == null || company.getNumbers().isEmpty()) {
				image = (ImageView) findViewById(R.id.phone);
				image.setVisibility(View.INVISIBLE);
			} else {
				phone = company.getNumbers().get(0).getCode() + ""
						+ company.getNumbers().get(0).getNumber();
			}
			if (company.getMap() == null
					|| company.getMap().trim().length() == 0) {
				image = (ImageView) findViewById(R.id.geo);
				image.setVisibility(View.INVISIBLE);
			} else {
				geo = company.getMap();
			}

		} catch (Exception ex) {
			Log.e("paintDetail", ex.getMessage());
		}

	}

	private void configClicks() {
		ImageView btnSend = (ImageView) findViewById(R.id.mail);
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(DetailItem.this, Mail.class);
				i.putExtra(Constants.MAIL_TO, mail);
				i.putExtra(Constants.LAYOUT_ID, R.layout.detailitem);
				i.putExtra(Constants.ID, ID_COMPANY);
				startActivityForResult(i, ACTIVITY_DETAIL_ITEM);
			}
		});

		btnSend = (ImageView) findViewById(R.id.phone);
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri
							.parse("tel:" + phone));
					startActivity(callIntent);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});

		btnSend = (ImageView) findViewById(R.id.web);
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					Intent callIntent = new Intent(Intent.ACTION_VIEW, Uri
							.parse("http://" + web + "/"));
					startActivity(callIntent);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});

		btnSend = (ImageView) findViewById(R.id.geo);
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					Intent i = new Intent(DetailItem.this, Map.class);
					i.putExtra("GEO", geo);
					startActivityForResult(i, ACTIVITY_DETAIL_ITEM);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
	}

	public void goHome() {

		ImageView home = (ImageView) findViewById(R.id.home);

		home.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(DetailItem.this, StartActivity.class);
				startActivityForResult(i, 1);
			}
		});

	}
}
